using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;

namespace <Database>
{
    public class <OwnerU>_<TableU>DataClass
    {
        public static DataTable SelectAll()
        {
            using (var context = new <Database>DBContext())
            {
                return context.<OwnerU>_<TableU>.ToList().ToDataTable<<OwnerU>_<TableU>>();
            }
        }

        public static <OwnerU>_<TableU> Select_Record(<OwnerU>_<TableU> _<OwnerU>_<TableU>)
        {
            using (var context = new <Database>DBContext())
            {
                return context.<OwnerU>_<TableU>.Find(
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                                                 _<OwnerU>_<TableU>.<FieldU>
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                                                , _<OwnerU>_<TableU>.<FieldU>
    </If Loop>
                                            );
            }
        }

        public static bool Add(<OwnerU>_<TableU> _<OwnerU>_<TableU>)
        {
            using (var context = new <Database>DBContext())
            {
                context.<OwnerU>_<TableU>.Add(_<OwnerU>_<TableU>);
                return context.SaveChanges() > 0;
            }
        }

        public static bool Update(<OwnerU>_<TableU> _<OwnerU>_<TableU>)
        {
            using (var context = new <Database>DBContext())
            {    
                context.<OwnerU>_<TableU>.Attach(_<OwnerU>_<TableU>);
                context.Entry(_<OwnerU>_<TableU>).State = System.Data.Entity.EntityState.Modified;
                return context.SaveChanges() > 0;
            }
        }

        public static bool Delete(<OwnerU>_<TableU> _<OwnerU>_<TableU>)
        {
            using (var context = new <Database>DBContext())
            {
                context.<OwnerU>_<TableU>.Remove(context.<OwnerU>_<TableU>.Find(
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                                                 _<OwnerU>_<TableU>.<FieldU>
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                                                , _<OwnerU>_<TableU>.<FieldU>
    </If Loop>
                                            ));
                return context.SaveChanges() > 0;
            }       
        }
   
    }
}
